package com.lwbldy.modules.system.controller;

import com.lwbldy.common.tools.R;
import com.lwbldy.common.tools.StringUtils;
import com.lwbldy.modules.system.domain.SysMenu;
import com.lwbldy.modules.system.domain.SysRole;
import com.lwbldy.modules.system.service.SysMenuService;
import com.lwbldy.modules.system.service.SysRoleService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("system/role")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysMenuService sysMenuService;

    /**
     * Describe: 基础路径
     * */
    private static String MODULE_PATH = "system/role/";

    /**
     * 角色主页面
     * @return
     */
    @GetMapping("main")
    @RequiresPermissions({"system:role:main"})
    public String list(){
        return MODULE_PATH + "main";
    }

    /**
     * 角色列表
     * @return
     */
    @PostMapping("list")
    @ResponseBody
    @RequiresPermissions({"system:role:main"})
    public R listJSON(){
        return R.ok(sysRoleService.listAll());
    }

    /**
     * 获取菜单新增视图
     * @return
     */
    @GetMapping("add")
    @RequiresPermissions({"system:role:save"})
    public String add(){
        return MODULE_PATH + "add";
    }

    /**
     * 保存
     * @param sysRole
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    @RequiresPermissions({"system:role:save"})
    public R save(@RequestBody SysRole sysRole){
        return R.resultRow(sysRoleService.save(sysRole));
    }

    /**
     * 获取角色修改视图
     * @param model
     * @param id
     * @return
     */
    @GetMapping("edit")
    @RequiresPermissions({"system:role:edit"})
    public String edit(Model model, long id){
        model.addAttribute("sysRole",sysRoleService.queryById(id));
        return MODULE_PATH + "edit";
    }

    /**
     * 修改菜单信息
     * @param sysRole
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    @RequiresPermissions({"system:role:edit"})
    public R update(@RequestBody SysRole sysRole){
        return R.resultRow(sysRoleService.update(sysRole));
    }


    /**
     * 根据id删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("remove/{id}")
    @ResponseBody
    @RequiresPermissions({"system:role:remove"})
    public R remove(@PathVariable long id){
        int result = sysRoleService.remove(id);
        return R.resultRow(result);
    }

    @DeleteMapping("removeBatch/{ids}")
    @ApiOperation(value="批量删除角色数据")
    @ResponseBody
    @RequiresPermissions({"system:role:remove"})
    public R removeBatch(@PathVariable String ids){
        int result = sysRoleService.removeBatch(StringUtils.StringIdsToArr(ids));
        return R.resultRow(result);
    }

    /**
     * Describe: 根据 Id 启用角色
     * Param: roleId
     * Return: ResuBean
     * */
    @PutMapping("enable")
    @ApiOperation(value="启用角色")
    @ResponseBody
    @RequiresPermissions({"system:role:edit"})
    public R enable(@RequestBody SysRole sysRole){
        sysRole.setStatus(1);
        return R.resultRow(sysRoleService.update(sysRole));
    }

    /**
     * Describe: 根据 Id 禁用角色
     * Param: roleId
     * Return: ResuBean
     * */
    @PutMapping("disable")
    @ApiOperation(value="禁用角色")
    @ResponseBody
    @RequiresPermissions({"system:role:edit"})
    public R disable(@RequestBody SysRole sysRole){
        sysRole.setStatus(0);
        return R.resultRow(sysRoleService.update(sysRole));
    }

    @GetMapping("power")
    @ApiOperation(value="获取分配角色权限视图")
    public String power(Model model, String roleId){
        model.addAttribute("roleId",roleId);
        return MODULE_PATH + "power";
    }

    /**
     * Describe: 获取角色权限
     * Param RoleId
     * Return ResuTree
     * */
    @GetMapping("getRolePower")
    @ApiOperation(value="获取角色权限数据")
    @ResponseBody
//    @RequiresPermissions({"system:role:power"})
    public R getRolePower(Long roleId){
        R r = R.ok(sysMenuService.queryMenuByRole(roleId));
        Map<String,Object> status = new HashMap<>();
        status.put("code",200);
        status.put("message","默认");
        r.put("status",status);
        return r;
    }

    @PutMapping("saveRolePower")
    @ApiOperation(value="获取角色权限数据")
    @ResponseBody
//    @RequiresPermissions({"system:role:power"})
    public R saveRolePower(Long roleId, String powerIds){
        List<Long> menuIds = StringUtils.StringToLongArr(powerIds.split(","));
        return R.resultRow(sysRoleService.saveRolePower(roleId,menuIds));
    }

}
